
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.AccountSecurityInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:更新安全邮箱
 *
 * @author jerryliu
 * @since 2015年8月20日 下午5:38:18
 */
public class UpdateSecurityEmailTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public UpdateSecurityEmailResponse request(String username, String kgToken, String email,
            String checkcode) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        req.setKgToken(kgToken);
        UpdateSecurityEmailResponse respObj = new UpdateSecurityEmailResponse();
        ResponsePackage resp = new UpdateSecurityEmailResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_UPDATE_SECURITY_EMAIL);
        params.put("username", username);
        params.put("kgtoken", kgToken);
        params.put("email", email);
        params.put("checkcode", checkcode);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_UPDATE_SECURITY_EMAIL);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class UpdateSecurityEmailResponsePackage extends
            HttpBaseResponsePackage<UpdateSecurityEmailResponse> {
        @Override
        protected void handleResponse(UpdateSecurityEmailResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "UpdateSecurityEmailTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_UPDATE_SECURITY_EMAIL.equalsIgnoreCase(cmd)) {
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    public static class UpdateSecurityEmailResponse extends ResponseMessage {
        private static final long serialVersionUID = -4820735216365527465L;

        private AccountSecurityInfo info;

        public AccountSecurityInfo getInfo() {
            return info;
        }

        public void setInfo(AccountSecurityInfo info) {
            this.info = info;
        }
    }
}
